//
//  ViewController.m
//  ReadDemo
//
//  Created by YPShao on 2017/10/17.
//  Copyright © 2017年 YPShao. All rights reserved.
//

#import "ViewController.h"
#import "ReadViewController.h"
#import "HMBrokenLine.h"
#import "Masonry.h"
#import <MediaPlayer/MediaPlayer.h>
#import "UVMyCellItem.h"
#import "VolumeViewController.h"
#import "HighlightViewController.h"
#import "DownloadViewController.h"

static NSString *const kMyTableCellIdentifier = @"UITableViewCell";

@interface ViewController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) NSMutableArray <UVMyCellItem *> *dataSource;
@property (nonatomic, strong) UITableView *tableView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self setupDataSource];
    [self setupContentView];

    
    HMBrokenLine *lineView = [[HMBrokenLine alloc]initWithFrame:CGRectMake(10, 200, 300, 10)];
    lineView.backgroundColor = [UIColor grayColor];
//    [self.view addSubview:lineView];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        lineView.lineColor = [UIColor greenColor];
    });
}

- (NSMutableArray *)dataSource
{
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

- (void)setupDataSource
{
    NSMutableArray<UVMyCellItem *> *dataSource = [NSMutableArray array];
    [dataSource addObject:[self createReadingCellItem]];
    [dataSource addObject:[self createVolumeChangeCellItem]];
    [dataSource addObject:[self createHighlightCellItem]];
    [dataSource addObject:[self createDownloadCellItem]];
    self.dataSource = dataSource;
}

- (void)setupContentView
{
    UITableView *tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    [self.view addSubview:tableView];
    tableView.delegate = self;
    tableView.dataSource = self;
    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kMyTableCellIdentifier];
    self.tableView = tableView;
}

- (UVMyCellItem *)createReadingCellItem
{
    __weak typeof(self) weakSelf = self;
    UVMyCellItem *readingRecordCellItem = [UVMyCellItem new];
    readingRecordCellItem.reuseIdentify = kMyTableCellIdentifier;
    readingRecordCellItem.getCellHeightBlock = ^CGFloat(NSIndexPath *indexPath, id data) {
        return 60;
    };
    readingRecordCellItem.updateBlock = ^(__kindof UITableViewCell *cell, NSIndexPath *indexPath, id data) {
        UITableViewCell *myTableCell = cell;
        myTableCell.textLabel.text = @"文本显示";
        
    };
    readingRecordCellItem.clickBlock = ^(NSIndexPath *indexPath, id data) {
        [weakSelf gotoReadView];
    };
    return readingRecordCellItem;
}

// 音量键调整
- (UVMyCellItem *)createVolumeChangeCellItem
{
    __weak typeof(self) weakSelf = self;
    UVMyCellItem *readingRecordCellItem = [UVMyCellItem new];
    readingRecordCellItem.reuseIdentify = kMyTableCellIdentifier;
    readingRecordCellItem.getCellHeightBlock = ^CGFloat(NSIndexPath *indexPath, id data) {
        return 60;
    };
    readingRecordCellItem.updateBlock = ^(__kindof UITableViewCell *cell, NSIndexPath *indexPath, id data) {
        UITableViewCell *myTableCell = cell;
        myTableCell.textLabel.text = @"音量键控制";

    };
    readingRecordCellItem.clickBlock = ^(NSIndexPath *indexPath, id data) {
        [weakSelf gotoVolumeVC];
    };
    return readingRecordCellItem;
    
}
// 关键词高亮
- (UVMyCellItem *)createHighlightCellItem
{
    __weak typeof(self) weakSelf = self;
    UVMyCellItem *readingRecordCellItem = [UVMyCellItem new];
    readingRecordCellItem.reuseIdentify = kMyTableCellIdentifier;
    readingRecordCellItem.getCellHeightBlock = ^CGFloat(NSIndexPath *indexPath, id data) {
        return 60;
    };
    readingRecordCellItem.updateBlock = ^(__kindof UITableViewCell *cell, NSIndexPath *indexPath, id data) {
        UITableViewCell *myTableCell = cell;
        myTableCell.textLabel.text = @"多个关键词高亮";
    };
    readingRecordCellItem.clickBlock = ^(NSIndexPath *indexPath, id data) {
        [weakSelf gotoHighlightVC];
    };
    return readingRecordCellItem;    
}

// 下载
- (UVMyCellItem *)createDownloadCellItem
{
    __weak typeof(self) weakSelf = self;
    UVMyCellItem *readingRecordCellItem = [UVMyCellItem new];
    readingRecordCellItem.reuseIdentify = kMyTableCellIdentifier;
    readingRecordCellItem.getCellHeightBlock = ^CGFloat(NSIndexPath *indexPath, id data) {
        return 60;
    };
    readingRecordCellItem.updateBlock = ^(__kindof UITableViewCell *cell, NSIndexPath *indexPath, id data) {
        UITableViewCell *myTableCell = cell;
        myTableCell.textLabel.text = @"多任务下载";
    };
    readingRecordCellItem.clickBlock = ^(NSIndexPath *indexPath, id data) {
        [weakSelf gotoDownloadVC];
    };
    return readingRecordCellItem;
}

- (void)gotoReadView
{
    ReadViewController *readVC = [[ReadViewController alloc]init];
    [self presentViewController:readVC animated:YES completion:nil];
}

- (void)gotoVolumeVC
{
    VolumeViewController *volumeVC = [[VolumeViewController alloc]init];
    [self.navigationController pushViewController:volumeVC animated:YES];
}

- (void)gotoHighlightVC
{
    HighlightViewController *newVC = [[HighlightViewController alloc]init];
    [self.navigationController pushViewController:newVC animated:YES];
}

- (void) gotoDownloadVC
{
    DownloadViewController *VC = [[DownloadViewController alloc]init];
    [self.navigationController  pushViewController:VC animated:YES];
}

#pragma mark - tableview

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UVMyCellItem *cellItem = self.dataSource[indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellItem.reuseIdentify];
//    cell.separatorInset = UIEdgeInsetsMake(0, 44, 0, 0);
//    cell.layoutMargins = UIEdgeInsetsMake(0, 44, 0, 0);
    if (cellItem.updateBlock) {
        cellItem.updateBlock(cell, indexPath, cellItem.data);
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    UVMyCellItem *cellItem = self.dataSource[indexPath.row];
    if (cellItem.clickBlock) {
        cellItem.clickBlock(indexPath, cellItem.data);
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
